package com.hiultra.archive.service.impl;

import com.hiultra.archive.domain.ArcDossier;
import com.hiultra.archive.domain.ArcFamily;
import com.hiultra.archive.mapper.ArcCatalogMapper;
import com.hiultra.archive.mapper.ArcDossierMapper;
import com.hiultra.archive.mapper.ArcFamilyMapper;
import com.hiultra.archive.service.IArcDossierService;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.core.domain.entity.ArcCatalog;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 卷宗管理Service业务层处理
 * 
 * @author ls
 * @date 2022-07-28
 */
@Service
public class ArcDossierServiceImpl implements IArcDossierService
{
    private static final Logger log = LoggerFactory.getLogger(ArcDossierServiceImpl.class);
    @Autowired
    private ArcDossierMapper arcDossierMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private ArcFamilyMapper arcFamilyMapper;
    @Autowired
    private ArcCatalogMapper arcCatalogMapper;

    /**
     * 查询卷宗管理
     * 
     * @param dossierId 卷宗管理主键
     * @return 卷宗管理
     */
    @Override
    public ArcDossier selectArcDossierByDossierId(Long dossierId)
    {
        return arcDossierMapper.selectArcDossierByDossierId(dossierId);
    }

    /**
     * 查询卷宗管理列表
     * 
     * @param arcDossier 卷宗管理
     * @return 卷宗管理
     */
    @Override
    public List<ArcDossier> selectArcDossierList(ArcDossier arcDossier)
    {
        return arcDossierMapper.selectArcDossierList(arcDossier);
    }

    /**
     * 新增卷宗管理
     * 
     * @param arcDossier 卷宗管理
     * @return 结果
     */
    @Override
    public int insertArcDossier(ArcDossier arcDossier)
    {
        return arcDossierMapper.insertArcDossier(arcDossier);
    }

    /**
     * 修改卷宗管理
     * 
     * @param arcDossier 卷宗管理
     * @return 结果
     */
    @Override
    public int updateArcDossier(ArcDossier arcDossier)
    {
        arcDossier.setUpdateTime(DateUtils.getNowDate());
        return arcDossierMapper.updateArcDossier(arcDossier);
    }

    /**
     * 批量删除卷宗管理
     * 
     * @param dossierIds 需要删除的卷宗管理主键
     * @return 结果
     */
    @Override
    public int deleteArcDossierByDossierIds(Long[] dossierIds)
    {
        return arcDossierMapper.deleteArcDossierByDossierIds(dossierIds);
    }

    /**
     * 删除卷宗管理信息
     * 
     * @param dossierId 卷宗管理主键
     * @return 结果
     */
    @Override
    public int deleteArcDossierByDossierId(Long dossierId)
    {
        return arcDossierMapper.deleteArcDossierByDossierId(dossierId);
    }

    /**
     * 导入卷宗数据
     *
     * @param dossierList 卷宗数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importDossier(List<ArcDossier> dossierList, Boolean isUpdateSupport, String operName){
        if (StringUtils.isNull(dossierList) || dossierList.size() == 0)
        {
            throw new ServiceException("导入卷宗数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ArcDossier dossier : dossierList)
        {
            try
            {
                //处理数据
                //根据全宗名称查询全宗对象
                ArcFamily arcFamily =arcFamilyMapper.selectArcFamilyByFamilyName(dossier.getFamily().getFamilyName());
                if(StringUtils.isNull(arcFamily)){
                    failureMsg.append("<br/>" + failureNum + "、全宗" + dossier.getFamily().getFamilyName() + " 不存在，请输入正确全宗信息");
                }
                dossier.setFamilyId(arcFamily.getFamilyId());

                //根据分类名称查询分类对象
                ArcCatalog arcCatalog = arcCatalogMapper.selectArcCatalogByCatalogName(dossier.getCatalog().getCatalogName());
                if(StringUtils.isNull(arcCatalog)){
                    failureMsg.append("<br/>" + failureNum + "、分类" + dossier.getCatalog().getCatalogName() + " 不存在，请输入正确分类信息");
                }
                dossier.setCatalogId(arcCatalog.getCatalogId());
                // 验证是否存在这个卷宗
                ArcDossier d = arcDossierMapper.selectDossierByDossierCode(dossier.getDossierCode());
                if (StringUtils.isNull(d))
                {
                    BeanValidators.validateWithException(validator, dossier);
                    dossier.setCreateBy(operName);
                    this.insertArcDossier(dossier);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、卷宗 " + dossier.getDossierName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, dossier);
                    dossier.setUpdateBy(operName);
                    this.updateArcDossier(dossier);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、卷宗 " + dossier.getDossierName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、卷宗 " + dossier.getDossierName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、卷宗 " + dossier.getDossierName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 校验案卷号称是否唯一
     *
     * @param arcDossier 案卷
     * @return 结果
     */
    @Override
    public String checkDossierCodeUnique(ArcDossier arcDossier){
        Long dossierId = StringUtils.isNull(arcDossier.getDossierId()) ? -1L : arcDossier.getDossierId();
        ArcDossier info = arcDossierMapper.checkDossierCodeUnique(arcDossier);
        if (StringUtils.isNotNull(info) && info.getDossierId().longValue() != dossierId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
